package com.cskaoyan.service;

import com.cskaoyan.exceptionJY.NameRepeatException;
import com.cskaoyan.exceptionJY.RoleExistException;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.bean.*;
import com.cskaoyan.model.bo.systembo.*;
import com.cskaoyan.model.vo.systemvo.*;
import com.cskaoyan.typehandler.IntegerArrayUtils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class SystemServiceImpl implements SystemService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    StorageMapper storageMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    LogMapper logMapper;

    @Override
    public AdminPageVo queryPage(Integer page, Integer limit, String sort, String order, String username) {
        AdminExample adminExample = new AdminExample();
        adminExample.setOrderByClause(sort + " " + order);
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);

        List<Admin> admins = adminMapper.selectByExample(adminExample);

        PageInfo<Admin> adminPageInfo = new PageInfo<>(admins);
        Long total = adminPageInfo.getTotal();

        AdminPageVo adminPageVo = new AdminPageVo();
        adminPageVo.setItems(admins);
        adminPageVo.setTotal(total);
        return adminPageVo;
    }

    @Override
    public Admin createAdmin(CreateAdminBo createAdminBo) throws NameRepeatException {
        AdminExample example = new AdminExample();
        List<Admin> admins = adminMapper.selectByExample(example);
        for (Admin admin : admins) {
            if (admin.getUsername().equals(createAdminBo.getUsername()))
                throw new NameRepeatException();
        }

        Admin admin = new Admin();
        admin.setUsername(createAdminBo.getUsername());
        admin.setPassword(createAdminBo.getPassword());
        admin.setRoleIds(createAdminBo.getRoleIds());
        admin.setAvatar(createAdminBo.getAvatar());
        admin.setAddTime("now");
        int id = adminMapper.insertSelective(admin);

        Admin admin1 = adminMapper.selectByPrimaryKey(id);

        return admin1;
    }

    @Override
    public void deleteAdmin(DeleteAdminBo deleteAdminBo) {
        Admin admin = new Admin();
        admin.setId(deleteAdminBo.getId());
        admin.setUsername(deleteAdminBo.getUsername());
        admin.setDeleted(true);
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public Admin updateAdmin(UpdateAdminBo updateAdminBo) throws NameRepeatException {

        Admin admin = new Admin();
        admin.setUsername(updateAdminBo.getUsername());
        admin.setPassword(updateAdminBo.getPassword());
        admin.setRoleIds(updateAdminBo.getRoleIds());
        admin.setAvatar(updateAdminBo.getAvatar());
        admin.setId(updateAdminBo.getId());
        admin.setUpdateTime("now");
        adminMapper.updateByPrimaryKeySelective(admin);

        Admin admin1 = adminMapper.selectByPrimaryKey(updateAdminBo.getId());
        return admin1;
    }

    @Override
    public RolePageVo roleList(Integer page, Integer limit, String sort, String order, String name) {
        RoleExample roleExample = new RoleExample();
        roleExample.setOrderByClause(sort + " " + order);
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        List<Role> roles = roleMapper.selectByExample(roleExample);

        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        long total = rolePageInfo.getTotal();

        RolePageVo rolePageVo = new RolePageVo();
        rolePageVo.setItems(roles);
        rolePageVo.setTotal(total);

        return rolePageVo;
    }

    @Override
    public Role roleCreate(CreateRoleBo createRoleBo) throws NameRepeatException {
        List<Role> roles = roleMapper.selectByExample(new RoleExample());
        for (Role role : roles) {
            if (role.getName().equals(createRoleBo.getName()))
                throw new NameRepeatException();
        }

        Role role = new Role();
        role.setDesc(createRoleBo.getDesc());
        role.setName(createRoleBo.getName());
        role.setDeleted(false);
        role.setEnabled(true);
        role.setAddTime("now");
        int id = roleMapper.insertSelective(role);

        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void roleDelete(DeleteRoleBo deleteRoleBo) throws RoleExistException {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        for (Admin admin : admins) {
            Integer[] roleIds = admin.getRoleIds();
            for (Integer roleId : roleIds) {
                if (roleId.equals(deleteRoleBo.getId()))
                    throw new RoleExistException();
            }
        }

        Role role = new Role();
        role.setId(deleteRoleBo.getId());
        role.setDeleted(true);
        role.setName(deleteRoleBo.getName());
        roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public ArrayList<OptionVo> roleOption() {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Role> roles = roleMapper.selectByExample(roleExample);

        ArrayList<OptionVo> optionVos = new ArrayList<>();

        for (Role role : roles) {
            OptionVo optionVo = new OptionVo();
            optionVo.setValue(role.getId());
            optionVo.setLabel(role.getName());
            optionVos.add(optionVo);
        }

        return optionVos;
    }

    @Override
    public void updateRole(UpdateRoleBo updateRoleBo) {
        Role role = new Role();
        role.setId(updateRoleBo.getId());
        role.setName(updateRoleBo.getName());
        role.setDesc(updateRoleBo.getDesc());
        role.setUpdateTime("now");
        roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public Storage storageCreate(MultipartFile file) {

        Storage storage = new Storage();
        String originalFilename = file.getOriginalFilename();
        File path = new File("d:\\mall28\\storage");
        if (!path.exists())
            path.mkdirs();
        //构造一个file来接收multipartFile
        File filepath = new File(path, originalFilename);//你想要以什么文件保存，第二个参数就写什么
        try {
            //将multipartFile转换为file,保存起来
            file.transferTo(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        storage.setKey(originalFilename);
        storage.setName(originalFilename);
        storage.setType(file.getContentType());
        storage.setSize((int) file.getSize());
        storage.setUrl("http://localhost:8083/mall28/storage/" + originalFilename);
        storage.setAddTime("now");
        storageMapper.insertSelective(storage);
        return storageMapper.selectByPrimaryKey(storage.getId());
    }

    @Override
    public StoragePageVo storageList(Integer page, Integer limit, String sort, String order, String key, String name) {
        StorageExample storageExample = new StorageExample();
        storageExample.setOrderByClause(sort + " " + order);
        StorageExample.Criteria criteria = storageExample.createCriteria();
        if (key != null && !"".equals(key)){
            criteria.andKeyLike("%" + key + "%");
        }
        if (name != null && !"".equals(name)){
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        List<Storage> storageList = storageMapper.selectByExample(storageExample);

        PageInfo<Storage> storagePageInfo = new PageInfo<>(storageList);
        long total = storagePageInfo.getTotal();

        StoragePageVo storagePageVo = new StoragePageVo();
        storagePageVo.setItems(storageList);
        storagePageVo.setTotal(total);
        return storagePageVo;
    }

    @Override
    public void storageDelete(DeleteStorageBo deleteStorageBo) {
        Storage storage = new Storage();
        storage.setId(deleteStorageBo.getId());
        storage.setDeleted(true);
        storageMapper.updateByPrimaryKeySelective(storage);
    }

    @Override
    public Storage storageUpdate(Storage storage) {
        Storage st = new Storage();
        st.setId(storage.getId());
        st.setName(storage.getName());

        storageMapper.updateByPrimaryKeySelective(st);

        return storageMapper.selectByPrimaryKey(storage.getId());
    }

    @Override
    public PermissionPageVo rolePerGet(Integer roleId) {
        PermissionPageVo permissionPageVo = new PermissionPageVo();

        List<String> permissions = permissionMapper.selectPermissionByRoleId(roleId);
        permissionPageVo.setAssignedPermissions(permissions);


        ArrayList<PermissionPageVo4> vo4List0 = new ArrayList<>();
        vo4List0.add(new PermissionPageVo4("POST /admin/admin/update", "admin:admin:update","编辑"));
        vo4List0.add(new PermissionPageVo4("GET /admin/admin/list","admin:admin:list","查询"));
        vo4List0.add(new PermissionPageVo4("GET /admin/admin/read","admin:admin:read","详情"));
        vo4List0.add(new PermissionPageVo4("POST /admin/admin/delete","admin:admin:delete","删除"));
        vo4List0.add(new PermissionPageVo4("POST /admin/admin/create","admin:admin:create","添加"));
        ArrayList<PermissionPageVo4> vo4List1 = new ArrayList<>();
        vo4List1.add(new PermissionPageVo4("POST /admin/role/permissions","admin:role:permission:update","权限变更"));
        vo4List1.add(new PermissionPageVo4("POST /admin/role/update","admin:role:update","角色编辑"));
        vo4List1.add(new PermissionPageVo4("GET /admin/role/list","admin:role:list","角色查询"));
        vo4List1.add(new PermissionPageVo4("GET /admin/role/read","admin:role:read","角色详情"));
        vo4List1.add(new PermissionPageVo4("POST /admin/role/delete","admin:role:delete","角色删除"));
        vo4List1.add(new PermissionPageVo4("GET /admin/role/permissions","admin:role:permission:get","权限详情"));
        vo4List1.add(new PermissionPageVo4("POST /admin/role/create","admin:role:create","角色添加"));
        ArrayList<PermissionPageVo4> vo4List2 = new ArrayList<>();
        vo4List2.add(new PermissionPageVo4("POST /admin/storage/update","admin:storage:update","编辑"));
        vo4List2.add(new PermissionPageVo4("GET /admin/storage/list","admin:storage:list","查询"));
        vo4List2.add(new PermissionPageVo4("POST /admin/storage/read","admin:storage:read","详情"));
        vo4List2.add(new PermissionPageVo4("POST /admin/storage/delete","admin:storage:delete","删除"));
        vo4List2.add(new PermissionPageVo4("POST /admin/storage/create","admin:storage:create","上传"));
        ArrayList<PermissionPageVo4> vo4List3 = new ArrayList<>();
        vo4List3.add(new PermissionPageVo4("GET /admin/log/list","admin:log:list","查询"));

        ArrayList<PermissionPageVo4> vo4List10 = new ArrayList<>();
        vo4List10.add(new PermissionPageVo4("POST /admin/brand/update","admin:brand:update","编辑"));
        vo4List10.add(new PermissionPageVo4("GET /admin/brand/list","admin:brand:list","查询"));
        vo4List10.add(new PermissionPageVo4("GET /admin/brand/read","admin:brand:read","详情"));
        vo4List10.add(new PermissionPageVo4("POST /admin/brand/delete","admin:brand:delete","删除"));
        vo4List10.add(new PermissionPageVo4("POST /admin/brand/create","admin:brand:create","添加"));
        ArrayList<PermissionPageVo4> vo4List11 = new ArrayList<>();
        vo4List11.add(new PermissionPageVo4("POST /admin/issue/update","admin:issue:update","编辑"));
        vo4List11.add(new PermissionPageVo4("GET /admin/issue/list","admin:issue:list","查询"));
        vo4List11.add(new PermissionPageVo4("POST /admin/issue/delete","admin:issue:delete","删除"));
        vo4List11.add(new PermissionPageVo4("POST /admin/issue/create","admin:issue:create","添加"));
        ArrayList<PermissionPageVo4> vo4List12 = new ArrayList<>();
        vo4List12.add(new PermissionPageVo4("POST /admin/order/refund","admin:order:refund","订单退款"));
        vo4List12.add(new PermissionPageVo4("POST /admin/order/ship","admin:order:ship","订单发货"));
        vo4List12.add(new PermissionPageVo4("GET /admin/order/detail","admin:order:read","详情"));
        vo4List12.add(new PermissionPageVo4("POST /admin/order/reply","admin:order:reply","订单商品回复"));
        vo4List12.add(new PermissionPageVo4("GET /admin/order/list","admin:order:list","查询"));
        ArrayList<PermissionPageVo4> vo4List13 = new ArrayList<>();
        vo4List13.add(new PermissionPageVo4("POST /admin/category/update","admin:category:update","编辑"));
        vo4List13.add(new PermissionPageVo4("GET /admin/category/list","admin:category:list","查询"));
        vo4List13.add(new PermissionPageVo4("GET /admin/category/read","admin:category:read","详情"));
        vo4List13.add(new PermissionPageVo4("POST /admin/category/delete","admin:category:delete","删除"));
        vo4List13.add(new PermissionPageVo4("POST /admin/category/create","admin:category:create","添加"));
        ArrayList<PermissionPageVo4> vo4List14 = new ArrayList<>();
        vo4List14.add(new PermissionPageVo4("POST /admin/keyword/update","admin:keyword:update","编辑"));
        vo4List14.add(new PermissionPageVo4("GET /admin/keyword/list","admin:keyword:list","查询"));
        vo4List14.add(new PermissionPageVo4("GET /admin/keyword/read","admin:keyword:read","详情"));
        vo4List14.add(new PermissionPageVo4("POST /admin/keyword/delete","admin:keyword:delete","删除"));
        vo4List14.add(new PermissionPageVo4("POST /admin/keyword/create","admin:keyword:create","添加"));
        ArrayList<PermissionPageVo4> vo4List20 = new ArrayList<>();
        vo4List20.add(new PermissionPageVo4("GET /admin/collect/list","admin:collect:list", "查询"));
        ArrayList<PermissionPageVo4> vo4List21 = new ArrayList<>();
        vo4List21.add(new PermissionPageVo4("GET /admin/footprint/list","admin:footprint:list","查询"));
        ArrayList<PermissionPageVo4> vo4List22 = new ArrayList<>();
        vo4List22.add(new PermissionPageVo4("GET /admin/feedback/list","admin:feedback:list","查询"));
        ArrayList<PermissionPageVo4> vo4List23 = new ArrayList<>();
        vo4List23.add(new PermissionPageVo4("GET /admin/history/list","admin:history:list","查询"));
        ArrayList<PermissionPageVo4> vo4List24 = new ArrayList<>();
        vo4List24.add(new PermissionPageVo4("GET /admin/user/list","admin:user:list","查询"));
        ArrayList<PermissionPageVo4> vo4List25 = new ArrayList<>();
        vo4List25.add(new PermissionPageVo4("GET /admin/address/list","admin:address:list","查询"));
        ArrayList<PermissionPageVo4> vo4List30 = new ArrayList<>();
        vo4List30.add(new PermissionPageVo4("GET /admin/config/wx","admin:config:wx:list","详情"));
        vo4List30.add(new PermissionPageVo4("POST /admin/config/wx","admin:config:wx:updateConfigs","编辑"));
        ArrayList<PermissionPageVo4> vo4List31 = new ArrayList<>();
        vo4List31.add(new PermissionPageVo4("GET /admin/config/express","admin:config:express:list","详情"));
        vo4List31.add(new PermissionPageVo4("POST /admin/config/express","admin:config:express:updateConfigs","编辑"));
        ArrayList<PermissionPageVo4> vo4List32 = new ArrayList<>();
        vo4List32.add(new PermissionPageVo4("GET /admin/config/mall","admin:config:mall:list","详情"));
        vo4List32.add(new PermissionPageVo4("POST /admin/config/mall","admin:config:mall:updateConfigs","编辑"));
        ArrayList<PermissionPageVo4> vo4List33 = new ArrayList<>();
        vo4List33.add(new PermissionPageVo4("GET /admin/config/order","admin:config:order:list","详情"));
        vo4List33.add(new PermissionPageVo4("POST /admin/config/order","admin:config:order:updateConfigs","编辑"));
        ArrayList<PermissionPageVo4> vo4List40 = new ArrayList<>();
        vo4List40.add(new PermissionPageVo4("POST /admin/ad/update","admin:ad:update","编辑"));
        vo4List40.add(new PermissionPageVo4("GET /admin/ad/list","admin:ad:list","查询"));
        vo4List40.add(new PermissionPageVo4("GET /admin/ad/read","admin:ad:read","详情"));
        vo4List40.add(new PermissionPageVo4("POST /admin/ad/delete","admin:ad:delete","删除"));
        vo4List40.add(new PermissionPageVo4("POST /admin/ad/create","admin:ad:create","添加"));
        ArrayList<PermissionPageVo4> vo4List41 = new ArrayList<>();
        vo4List41.add(new PermissionPageVo4("POST /admin/topic/update","admin:topic:update","编辑"));
        vo4List41.add(new PermissionPageVo4("GET /admin/topic/list","admin:topic:list","查询"));
        vo4List41.add(new PermissionPageVo4("GET /admin/topic/read","admin:topic:read","详情"));
        vo4List41.add(new PermissionPageVo4("POST /admin/topic/delete","admin:topic:delete","删除"));
        vo4List41.add(new PermissionPageVo4("POST /admin/topic/create","admin:topic:create","添加"));
        ArrayList<PermissionPageVo4> vo4List42 = new ArrayList<>();
        vo4List42.add(new PermissionPageVo4("GET /admin/coupon/listuser","admin:coupon:listuser","查询用户"));
        vo4List42.add(new PermissionPageVo4("POST /admin/coupon/update","admin:coupon:update","编辑"));
        vo4List42.add(new PermissionPageVo4("GET /admin/coupon/list","admin:coupon:list","查询"));
        vo4List42.add(new PermissionPageVo4("GET /admin/coupon/read","admin:coupon:read","详情"));
        vo4List42.add(new PermissionPageVo4("POST /admin/coupon/delete","admin:coupon:delete","删除"));
        vo4List42.add(new PermissionPageVo4("POST /admin/coupon/create","admin:coupon:create","添加"));
        ArrayList<PermissionPageVo4> vo4List43 = new ArrayList<>();
        vo4List43.add(new PermissionPageVo4("GET /admin/groupon/listRecord","admin:groupon:read","详情"));
        vo4List43.add(new PermissionPageVo4("POST /admin/groupon/update","admin:groupon:update","编辑"));
        vo4List43.add(new PermissionPageVo4("GET /admin/groupon/list","admin:groupon:list","查询"));
        vo4List43.add(new PermissionPageVo4("POST /admin/groupon/delete","admin:groupon:delete","删除"));
        vo4List43.add(new PermissionPageVo4("POST /admin/groupon/create","admin:groupon:create","添加"));

        ArrayList<PermissionPageVo4> vo4List50 = new ArrayList<>();
        vo4List50.add(new PermissionPageVo4("GET /admin/goods/detail","admin:goods:read","详情"));
        vo4List50.add(new PermissionPageVo4("POST /admin/goods/update","admin:goods:update","编辑"));
        vo4List50.add(new PermissionPageVo4("GET /admin/goods/list","admin:goods:list","查询"));
        vo4List50.add(new PermissionPageVo4("POST /admin/goods/delete","admin:goods:delete","删除"));
        vo4List50.add(new PermissionPageVo4("POST /admin/goods/create","admin:goods:create","上架"));
        ArrayList<PermissionPageVo4> vo4List51 = new ArrayList<>();
        vo4List51.add(new PermissionPageVo4("GET /admin/comment/list","admin:comment:list","查询"));
        vo4List51.add(new PermissionPageVo4("POST /admin/comment/delete","admin:comment:delete","删除"));

        ArrayList<PermissionPageVo4> vo4List60 = new ArrayList<>();
        vo4List60.add(new PermissionPageVo4("POST /admin/index/write","index:permission:write","权限写"));
        vo4List60.add(new PermissionPageVo4("GET /admin/index/read","index:permission:read","权限读"));
        ArrayList<PermissionPageVo4> vo4List70 = new ArrayList<>();
        vo4List70.add(new PermissionPageVo4("GET /admin/stat/user","admin:stat:user","查询"));
        ArrayList<PermissionPageVo4> vo4List71 = new ArrayList<>();
        vo4List71.add(new PermissionPageVo4("GET /admin/stat/order","admin:stat:order","查询"));
        ArrayList<PermissionPageVo4> vo4List72 = new ArrayList<>();
        vo4List72.add(new PermissionPageVo4("GET /admin/stat/goods","admin:stat:goods","查询"));


        ArrayList<PermissionPageVo3> vo3List0 = new ArrayList<>();
        vo3List0.add(new PermissionPageVo3("管理员管理", "管理员管理",vo4List0));
        vo3List0.add(new PermissionPageVo3("角色管理","角色管理",vo4List1));
        vo3List0.add(new PermissionPageVo3("对象存储","对象存储",vo4List2));
        vo3List0.add(new PermissionPageVo3("操作日志","操作日志",vo4List3));
        ArrayList<PermissionPageVo3> vo3List1 = new ArrayList<>();
        vo3List1.add(new PermissionPageVo3("品牌管理","品牌管理",vo4List10));
        vo3List1.add(new PermissionPageVo3("通用问题","通用问题",vo4List11));
        vo3List1.add(new PermissionPageVo3("订单管理","订单管理",vo4List12));
        vo3List1.add(new PermissionPageVo3("类目管理","类目管理",vo4List13));
        vo3List1.add(new PermissionPageVo3("关键词","关键词",vo4List14));
        ArrayList<PermissionPageVo3> vo3List2 = new ArrayList<>();
        vo3List2.add(new PermissionPageVo3("用户收藏","用户收藏",vo4List20));
        vo3List2.add(new PermissionPageVo3("用户足迹","用户足迹",vo4List21));
        vo3List2.add(new PermissionPageVo3("意见反馈","意见反馈",vo4List22));
        vo3List2.add(new PermissionPageVo3("搜索历史","搜索历史",vo4List23));
        vo3List2.add(new PermissionPageVo3("会员管理","会员管理",vo4List24));
        vo3List2.add(new PermissionPageVo3("收货地址","收货地址",vo4List25));

        ArrayList<PermissionPageVo3> vo3List3 = new ArrayList<>();
        vo3List3.add(new PermissionPageVo3("小程序配置","小程序配置",vo4List30));
        vo3List3.add(new PermissionPageVo3("运费配置","运费配置",vo4List31));
        vo3List3.add(new PermissionPageVo3("商场配置","商场配置",vo4List32));
        vo3List3.add(new PermissionPageVo3("订单配置","订单配置",vo4List33));

        ArrayList<PermissionPageVo3> vo3List4 = new ArrayList<>();
        vo3List4.add(new PermissionPageVo3("广告管理","广告管理",vo4List40));
        vo3List4.add(new PermissionPageVo3("专题管理","专题管理",vo4List41));
        vo3List4.add(new PermissionPageVo3("优惠券管理","优惠券管理",vo4List42));
        vo3List4.add(new PermissionPageVo3("团购管理","团购管理",vo4List43));

        ArrayList<PermissionPageVo3> vo3List5 = new ArrayList<>();
        vo3List5.add(new PermissionPageVo3("商品管理","商品管理",vo4List50));
        vo3List5.add(new PermissionPageVo3("评论管理","评论管理",vo4List51));
        ArrayList<PermissionPageVo3> vo3List6 = new ArrayList<>();
        vo3List6.add(new PermissionPageVo3("权限测试","权限测试",vo4List60));
        ArrayList<PermissionPageVo3> vo3List7 = new ArrayList<>();
        vo3List7.add(new PermissionPageVo3("用户统计","用户统计",vo4List70));
        vo3List7.add(new PermissionPageVo3("订单统计","订单统计",vo4List71));
        vo3List7.add(new PermissionPageVo3("商品统计","商品统计",vo4List72));


        ArrayList<PermissionPageVo2> systemPermissions = new ArrayList<>();
        systemPermissions.add(new PermissionPageVo2("系统管理", "系统管理",vo3List0));
        systemPermissions.add(new PermissionPageVo2("商场管理","商场管理",vo3List1));
        systemPermissions.add(new PermissionPageVo2("用户管理","用户管理",vo3List2));
        systemPermissions.add(new PermissionPageVo2("配置管理","配置管理",vo3List3));
        systemPermissions.add(new PermissionPageVo2("推广管理","推广管理",vo3List4));
        systemPermissions.add(new PermissionPageVo2("商品管理","商品管理",vo3List5));
        systemPermissions.add(new PermissionPageVo2("其他","其他",vo3List6));
        systemPermissions.add(new PermissionPageVo2("统计管理","统计管理",vo3List7));

        permissionPageVo.setSystemPermissions(systemPermissions);
        return permissionPageVo;
    }

    @Override
    public void rolePerPost(PermissionBo permissionBo) {
        String[] permissions = permissionBo.getPermissions();
        permissionMapper.deleteByRoleId(permissionBo.getRoleId());
        for (String permission : permissions) {
            Permission per = new Permission();
            per.setRoleId(permissionBo.getRoleId());
            per.setAddTime("now");
            per.setAddTime("now");
            per.setPermission(permission);
            permissionMapper.insertSelective(per);
        }
    }

    @Override
    public LogPageVo logList(Integer page, Integer limit, String sort, String order, String name) {
        LogExample logExample = new LogExample();
        logExample.setOrderByClause(sort + " " + order);
        LogExample.Criteria criteria = logExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andAdminLike("%" + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<Log> logs = logMapper.selectByExample(logExample);

        PageInfo<Log> logPageInfo = new PageInfo<>(logs);
        long total = logPageInfo.getTotal();

        LogPageVo logPageVo = new LogPageVo();
        logPageVo.setItems(logs);
        logPageVo.setTotal(total);
        return logPageVo;
    }

    @Override
    public AuthInfoVo authInfo(String token) {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        AuthInfoVo authInfoVo = new AuthInfoVo();

        authInfoVo.setName(username);
        String avatar = adminMapper.selectAvatarByUsername(username);
        authInfoVo.setAvatar(avatar);

        String integersS = adminMapper.selectRoleIdByUsername(username);
        Integer[] integers = IntegerArrayUtils.change(integersS);
        ArrayList<String> roles = new ArrayList<>();
        for (Integer integer : integers) {
            String name = roleMapper.selectNameByRoleId(integer);
            roles.add(name);
        }
        authInfoVo.setRoles(roles);

        String s = adminMapper.selectRoleIdByUsername(username);
        Integer[] roleIds = IntegerArrayUtils.change(s);
        HashSet<String> allPermission = new HashSet<>();
        for (Integer roleId : roleIds) {
            List<String> permissions = permissionMapper.selectPermissionByRoleId(roleId);
            allPermission.addAll(permissions);
        }

        HashSet<String> apis = new HashSet<>();
        if ("admin123".equals(username)){
            apis.add("*");
            authInfoVo.setPerms(apis);
            return authInfoVo;
        }

        for (String per : allPermission) {
            String api = permissionMapper.selectApiByPermission(per);
            apis.add(api);
        }


        authInfoVo.setPerms(apis);

        return authInfoVo;
    }


}


